<?php
//模型
class ArticleModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'article';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = " WHERE 1 ";
		
		
		if(isset($arr['type']) && !empty($arr['type'])){
			$where .= " AND a.type_id='{$arr['type']}' ";
		}
		
		
		$sql  = "SELECT a.*,t.name AS type_name,u.nickname,u.headimg from ".
				$this->db->dbprefix.$this->table." AS a LEFT JOIN ".
			    $this->db->dbprefix."article_type AS t ON a.type_id=t.id LEFT JOIN ".
			    $this->db->dbprefix."user AS u ON a.user_id=u.id {$where} ORDER BY a.addtime DESC LIMIT ".($page-1)*$size .",{$size}";
		
		$query = $this->db->query($sql);
		$res = $query->result_array();

		
		foreach($res as $k=>$v){
			$is_like = 0;
			$is_collection = 0;
			$is_share = 0;
			if(isset($arr['user_id']) && !empty($arr['user_id'])){
				$sql  = "select * from ".$this->db->dbprefix."article_active where aid='{$v['id']}' and user_id='{$arr['user_id']}' ";
				$query = $this->db->query($sql);
				$arow = $query->row_array();
				if($arow){
					$is_like = $arow['likes'];
					$is_collection = $arow['collection'];
					$is_share = $arow['share'];
				}
			}
			$res[$k]['is_like'] = $is_like;
			$res[$k]['is_collection'] = $is_collection;
			$res[$k]['is_share'] = $is_share;
			
			$res[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
		}
		
		$sql  = "SELECT count(a.id) as num from ".
				$this->db->dbprefix.$this->table." AS a LEFT JOIN ".
			    $this->db->dbprefix."article_type AS t ON a.type_id=t.id LEFT JOIN ".
			    $this->db->dbprefix."user AS u ON a.user_id=u.id {$where} ";
				
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists(){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table;
		$query = $this->db->query($sql);
		$res = $query->result_array();
		return $res;		
	}
	//详情
	function get_info($id,$user_id=0){
		$sql  = "SELECT a.*,t.name AS type_name,u.nickname,u.headimg from ".
				$this->db->dbprefix.$this->table." AS a LEFT JOIN ".
			    $this->db->dbprefix."article_type AS t ON a.type_id=t.id LEFT JOIN ".
			    $this->db->dbprefix."user AS u ON a.user_id=u.id where a.id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		
		if(!$row){
			return false;
		}
		
		$is_like = 0;
		$is_collection = 0;
		$is_share = 0;
		if(!empty($user_id)){
			$sql  = "select * from ".$this->db->dbprefix."article_active where aid='{$row['id']}' and user_id='{$user_id}' ";
			$query = $this->db->query($sql);
			$arow = $query->row_array();
			if($arow){
				$is_like = $arow['likes'];
				$is_collection = $arow['collection'];
				$is_share = $arow['share'];
			}
		}
		$row['is_like'] = $is_like;
		$row['is_collection'] = $is_collection;
		$row['is_share'] = $is_share;
		
		$row['addtime'] = date("Y-m-d H:i:s",$row['addtime']);
		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

